//
//  mluLawyerSpheresEdit.m
//  mluLawyer
//
//  Created by Paulo Ferreira on 12/1/09.
//  Copyright 2009 MobileLifeUtils.com. All rights reserved.
//

#import "mluLawyerSpheresEdit.h"


@implementation mluLawyerSpheresEdit

@synthesize mocSpheresEdit, maLabels, maFields, tfCurrent, edSphere;

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}

- (void)viewDidLoad {
	
	UIBarButtonItem * bbiDelete = [[UIBarButtonItem alloc] 
								   initWithTitle:NSLocalizedString(@"btDelete", nil)
								   style:UIBarButtonItemStyleBordered
								   target:self 
								   action:@selector(deleteCaseSituation)
								   ];
	
	
	UIBarButtonItem * bbiSave = [[UIBarButtonItem alloc] 
								 initWithTitle:NSLocalizedString(@"btSave", nil)
								 style:UIBarButtonItemStyleBordered
								 target:self 
								 action:@selector(saveCaseSituation)
								 ];
	
	UILabel *lblSphere = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 10.0, 280.0, 20.0)];
	[lblSphere setText:NSLocalizedString(@"lblSphere", nil)];
	
	UITextField *tfSphere = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 35.0, 280.0, 30.0)];
	tfSphere.delegate = self;
	[tfSphere setBorderStyle:UITextBorderStyleLine];
	[tfSphere setReturnKeyType:UIReturnKeyDone];
	tfSphere.clearButtonMode = UITextFieldViewModeWhileEditing;
	
	
	if (edSphere == nil) {
		self.title = NSLocalizedString(@"titEditNew", nil);
		[bbiDelete setEnabled:FALSE];
	} else {
		self.title = NSLocalizedString(@"titEdit", nil);
		tfCaseSituation.text = edCaseSituation.name;
	}
	
	maLabels = [[NSMutableArray alloc] initWithObjects:lblSphere, nil];
	maFields = [[NSMutableArray alloc] initWithObjects:tfSphere, nil];
	
	self.toolbarItems = [[NSArray alloc] initWithObjects:bbiDelete, bbiSave, nil];
	[super viewDidLoad];
}

/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */
/*
 - (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 }
 */
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark UITableViewDelegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [maLabels count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    [cell addSubview:[maLabels objectAtIndex:indexPath.row]];
	[cell addSubview:[maFields objectAtIndex:indexPath.row]];
	
	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 80.0;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark -
#pragma mark UITextFieldDelegate methods

- (void) textFieldDidBeginEditing:(UITextField *)textField {
	tfCurrent = nil;
	tfCurrent = textField;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[self endEditing];
	return YES;
}

#pragma mark -
#pragma mark UIScrollViewDelegate methods

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
	[self endEditing];
}

#pragma mark -
#pragma mark Custom methods

- (void)endEditing {
	if (tfCurrent != nil) {
		[tfCurrent resignFirstResponder];
	}
}

- (void)saveSphere {
	
	if(edCaseSituation == nil) {
		edCaseSituation = [NSEntityDescription 
						   insertNewObjectForEntityForName:@"case_situations" 
						   inManagedObjectContext:mocCaseSituationsEdit
						   ];
		
	}
	
	UITextField *tfCaseSituation = [maFields objectAtIndex:0];
	[edCaseSituation setName:tfCaseSituation.text];
	
	NSError *errPersistence;
	if(![mocCaseSituationsEdit save:&errPersistence]) {
		NSLog(@"%@", [errPersistence localizedDescription]);
	} else {
		[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
	}
}

@end

